package com.ljfiction.book.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.baidu.mobstat.StatService;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.FistHom;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.HttpParameter;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.model.HttpHeaders;

import okhttp3.Call;
import okhttp3.Response;


public class StartActivity extends BaseActivity {

    private FistHom f;
    private boolean user_first;

    private SharedPreferences setting;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.put("token",SettingManager.getInstance().getSessionId());
        httpHeaders.put("equipmentIdentification", SettingManager.getInstance().getUUID());
        httpHeaders.put("equipmentName",  android.os.Build.BRAND);
        httpHeaders.put("equipmentType",  android.os.Build.MODEL );
        OkHttpUtils.getInstance().addCommonHeaders(httpHeaders);
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        StatService.start(this);
        init();
    }

    private void init() {

            //后台登录
        UserManageBean.backgroundLogin(StartActivity.this);
        setting = getSharedPreferences("lajixs", 0);
        user_first = setting.getBoolean("FIRST", true);
        if (!user_first) {
            gethotdata();
        }
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if (user_first) {//第一次
                    Intent intent = new Intent(StartActivity.this, YinDaoActivity.class);
                    startActivity(intent);
                    setting.edit().putBoolean("FIRST", false).commit();
                } else {
                    Intent intent = new Intent(StartActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("firstHom", f);
                    startActivity(intent);
                    setting.edit().commit();
                }
                goBack();
            }
        }, 3000); // 启动动画持续3秒钟
        //关闭不必要的Activity
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler!=null){
            handler.removeCallbacksAndMessages(null);
        }


    }

    private void gethotdata() {
        OkHttpUtils.get(HttpManager.IP + "/hot/homePageRecommended")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson g = new Gson();
                        try {
                            f = g.fromJson(s, FistHom.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            //case REQUEST_EXTERNAL_STORAGE_PERMISSION:
            case 1:
                if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                }
                break;
            default:
        }
    }
}
